package com.linkage.jz.bankruptcy.activity.Project;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.adapter.ZQPreviewItemAdapter;
import com.linkage.jz.bankruptcy.adapter.ZQPreviewItemHeaderImpl;
import com.linkage.jz.bankruptcy.adapter.ZQPreviewItemSubmitImpl;
import com.linkage.jz.bankruptcy.adapter.ZQPreviewModelAdapter;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.bean.ZQPreviewCategory;
import com.linkage.jz.bankruptcy.bean.ZQPreviewDetail;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ZQPreviewModelActivity extends BaseActivity {

    private static final String KEY_DISPLAY_DATAS = "KEY_DISPLAY_DATAS";
    private static final String KEY_PREVIEW_HEADER = "KEY_PREVIEW_HEADER";
    private static final String KEY_PREVIEW_HEADER_ACTION = "KEY_PREVIEW_HEADER_ACTION";
    private static final String KEY_PREVIEW_TITLE = "KEY_PREVIEW_TITLE";
    private static final String KEY_PREVIEW_BUTTON = "KEY_PREVIEW_BUTTON";
    private static final String KEY_PREVIEW_BUTTON_ACTION = "KEY_PREVIEW_BUTTON_ACTION";
    private static final String KEY_PREVIEW_HEADER_BUILDER = "KEY_PREVIEW_HEADER_BUILDER";
    private static final String KEY_PREVIEW_BUTTON_BUILDER = "KEY_PREVIEW_BUTTON_BUILDER";

    @BindView(R.id.rv)
    RecyclerView mRecyclerView;
    @BindView(R.id.top_title)
    TextView mTitleView;

    public void start(Context context,
                      List<ZQPreviewCategory> previewDatas, String title,
                      String header, int headerAction, String buttonStr, int buttonAction) {
        ArrayList<ZQPreviewDetail> data = new ArrayList<>();
        for (ZQPreviewCategory category : previewDatas) {
            if (!category.isIgnore()) {
                data.add(category.switchToLabelDetail());
            }
            if (category.getDetailList() != null) data.addAll(category.getDetailList());
        }
        Intent intent = new Intent(context, ZQPreviewModelActivity.class);
        intent.putParcelableArrayListExtra(KEY_DISPLAY_DATAS, data);
        intent.putExtra(KEY_PREVIEW_TITLE, title);
        intent.putExtra(KEY_PREVIEW_HEADER, header);
        intent.putExtra(KEY_PREVIEW_HEADER_ACTION, headerAction);
        intent.putExtra(KEY_PREVIEW_BUTTON, buttonStr);
        intent.putExtra(KEY_PREVIEW_BUTTON_ACTION, buttonAction);
        context.startActivity(intent);
    }

    public void start(Context context,
                      List<ZQPreviewCategory> previewDatas, String title,
                      Class<? extends ZQPreviewItemAdapter> headerAdapterClazz, @NonNull Class<? extends ZQPreviewItemAdapter> submitBtnAdapterClazz) {
        ArrayList<ZQPreviewDetail> data = new ArrayList<>();
        for (ZQPreviewCategory category : previewDatas) {
            if (!category.isIgnore()) {
                data.add(category.switchToLabelDetail());
            }
            if (category.getDetailList() != null) data.addAll(category.getDetailList());
        }
        Intent intent = new Intent(context, ZQPreviewModelActivity.class);
        intent.putParcelableArrayListExtra(KEY_DISPLAY_DATAS, data);
        intent.putExtra(KEY_PREVIEW_TITLE, title);
        intent.putExtra(KEY_PREVIEW_HEADER_BUILDER, headerAdapterClazz);
        intent.putExtra(KEY_PREVIEW_BUTTON_BUILDER, submitBtnAdapterClazz);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_zq_preview);
        super.onCreate(savedInstanceState);



    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void initTitle() {
        mTitleView.setText(getIntent().getStringExtra(KEY_PREVIEW_TITLE));
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        ZQPreviewModelAdapter zqPreviewModelAdapter = null;
        Class<? extends ZQPreviewItemAdapter> submitBtnAdapterClazz =
                (Class<? extends ZQPreviewItemAdapter>) getIntent().getSerializableExtra(KEY_PREVIEW_BUTTON_BUILDER);
        if (submitBtnAdapterClazz != null) {
            try {
                ZQPreviewItemAdapter headerAdapter = null;
                Class<? extends ZQPreviewItemAdapter> headerAdapterClazz =
                        (Class<? extends ZQPreviewItemAdapter>) getIntent().getSerializableExtra(KEY_PREVIEW_HEADER_BUILDER);
                if (headerAdapterClazz != null) headerAdapter = headerAdapterClazz.newInstance();
                zqPreviewModelAdapter = new ZQPreviewModelAdapter.Builder(this)
                        .setPreviewDatas(getIntent().getParcelableArrayListExtra(KEY_DISPLAY_DATAS))
                        .setZqPreviewSubmitBtn(submitBtnAdapterClazz.newInstance())
                        .setZqPreviewHeader(headerAdapter)
                        .build();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        } else {
            ZQPreviewItemAdapter subAdapter = new ZQPreviewItemSubmitImpl(getIntent().getStringExtra(KEY_PREVIEW_BUTTON),
                    getIntent().getIntExtra(KEY_PREVIEW_BUTTON_ACTION, -1));
            ZQPreviewItemAdapter headerAdapter = new ZQPreviewItemHeaderImpl(getIntent().getStringExtra(KEY_PREVIEW_HEADER),
                    getIntent().getIntExtra(KEY_PREVIEW_HEADER_ACTION, -1));
            zqPreviewModelAdapter = new ZQPreviewModelAdapter.Builder(this)
                    .setPreviewDatas(getIntent().getParcelableArrayListExtra(KEY_DISPLAY_DATAS))
                    .setZqPreviewSubmitBtn(subAdapter)
                    .setZqPreviewHeader(headerAdapter)
                    .build();
        }
        if (zqPreviewModelAdapter != null) mRecyclerView.setAdapter(zqPreviewModelAdapter);
    }
}
